package com.aijk.web.admin.controller.medical;

import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.commons.utils.Collections3;
import com.aijk.commons.utils.Utils;
import com.aijk.medical.api.FdStatisticApi;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.entity.Constant;
import com.aijk.web.admin.utils.DataTableUtil;
import com.github.pagehelper.PageInfo;
import com.lifesea.familydoctor.api.FdTeamApi;
import com.lifesea.familydoctor.model.FdTeam;
import com.aijk.medical.model.ext.FdFdStatisticsMember;
import com.aijk.medical.model.qo.FdStatisticsQo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

import static sun.net.ftp.FtpReplyCode.find;

/**
 * Created by Quest on 2017/9/22.
 */
@Controller
@RequestMapping("/medical/statistic")
public class FdStatisticController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(FdStatisticController.class);
    @Autowired
    private FdStatisticApi statisticApi;
    @Autowired
    private FdTeamApi teamApi;

    /**
     * @Description: 进入团队页面
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list_view(Model model) {
        //默认获取当前团队服务记录
        HttpSession session = request.getSession();
        FdTeam teamInfo = (FdTeam) session.getAttribute(Constant.SESSION_TEAM);
        if (teamInfo != null) {
            model.addAttribute("teamId", teamInfo.getTeamId());
            model.addAttribute("teamName", teamInfo.getTeamName());
        }else{
            model.addAttribute("msg", "身份信息已失效，请重新登录！");
            return "/error/error_msg";
        }
        model.addAttribute("idcard",(String) session.getAttribute(Constant.SESSION_IDCARD));
        return "medical/statistic/list";
    }
    /**
     * @Description: 加载家医所属团队列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg list(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Integer draw) {
        DataTableUtil.FormatOptions(skey);
        if (Utils.isNotEmpty(skey.get("orderbyclause"))) {
            skey.put("orderbyclause", StringUtil.toUnderlineCase(Utils.toString(skey.get("orderbyclause"))));
        }
        FdStatisticsQo qo = DataTableUtil.fillQo(skey, FdStatisticsQo.class);
        PageInfo<FdFdStatisticsMember> memberPageInfo = statisticApi.statisticsMember(qo);
        if (Collections3.isNotEmpty(memberPageInfo.getList())) {
            List<FdFdStatisticsMember> memberList = addSignToTeamLeader(memberPageInfo.getList(), qo.getTeamId());
            FdFdStatisticsMember total = totalRowMember(memberList);
            memberPageInfo.getList().add(0, total);
        }
        RetMsg regMsg = new RetMsg();
        regMsg.put("data", memberPageInfo.getList());
        regMsg.put("recordsTotal", memberPageInfo.getTotal());
        regMsg.put("recordsFiltered", memberPageInfo.getTotal());
        regMsg.put("draw", draw);

        return regMsg;
    }
    private List<FdFdStatisticsMember> addSignToTeamLeader(List<FdFdStatisticsMember> list, Integer teamId){
        //统计团队中不存在的成员签约的居民数（由web自动作为团队长的签约数）
        int signNum = statisticApi.countSignWithoutServant(teamId);
        int teamLeaderId = 0;
        FdTeam team = teamApi.find(teamId);
        if(team != null){
            teamLeaderId = team.getTeamLeaderId();
        }
        for(FdFdStatisticsMember item: list){
            if(item.getId().equals(teamLeaderId)){
                item.setSignNum(item.getSignNum() + signNum);
            }
        }
        return list;
    }
    private FdFdStatisticsMember totalRowMember(List<FdFdStatisticsMember> list) {
        int signNumTotal = 0;
        FdFdStatisticsMember bean = new FdFdStatisticsMember();

        for (FdFdStatisticsMember item: list) {
            signNumTotal += (item.getSignNum() == null ? 0 : item.getSignNum());
        }
        bean.setTitle("合计");
        bean.setSignNum(signNumTotal);
        return bean;
    }

}
